package com.me.springcloud.feign.ssl;

import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import feign.Client;
import feign.Feign;
import feign.Retryer;
import feign.auth.BasicAuthRequestInterceptor;

public class FeignSSLTest {

	public static void main(String[] args) throws Exception {
		System.setProperty("jsse.enableSNIExtension", "false");

		Client disableHostnameVerification = new Client.Default(TrustingSSLSocketFactory2.create(), new HostnameVerifier() {
			@Override
			public boolean verify(String s, SSLSession sslSession) {
				return true;
			}
		});

		QuickPayService service = Feign.builder()//
				.client(disableHostnameVerification)//
				.requestInterceptor(new BasicAuthRequestInterceptor("812310063001078", "vpos123"))//
				.retryer(Retryer.NEVER_RETRY)//
				.target(QuickPayService.class, "https://mas.99bill.com:443");//

		String response = service.getDynNum(getData());
		System.out.println("response = " + response);
	}

	public static String getData() throws Exception {

		StringBuilder st = new StringBuilder();
		st.append("<MasMessage xmlns=\"http://www.99bill.com/mas_cnp_merchant_interface\">");
		CommonUtil.appXml("version", "1.0", st);
		CommonUtil.startXml("GetDynNumContent", st);

		CommonUtil.appXml("pan", "4338348788888812347898", st);
		CommonUtil.appXml("cardHolderName", "王三小", st);
		CommonUtil.appXml("idType", "0", st);
		CommonUtil.appXml("cardHolderId", "370101198106270984", st);

		CommonUtil.appXml("phoneNO", "12889328141", st);
		CommonUtil.appXml("amount", "10", st);
		CommonUtil.appXml("externalRefNumber", "201702500000000000000020976329", st);
		CommonUtil.appXml("merchantId", "812310063001078", st);
		CommonUtil.appXml("customerId", "812310063001078", st);

		CommonUtil.endXml("GetDynNumContent", st);
		st.append("</MasMessage>");

		System.out.println(st);
		return st.toString();
	}

	public static Map<String, String> getPrdEnvParamMap(String method, String billDate) {
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("domain_name", "https://mas.99bill.com");
		paramMap.put("ssl_port", "443");
		paramMap.put("method", "/cnp/getDynNum");
		paramMap.put("merchantId", "812310063001078");
		paramMap.put("txnDate", billDate);
		paramMap.put("cert_file_name", "D:\\Java\\workspace_epay_17_6_6\\EPayment\\EPayment-web\\profiles\\prd\\cer\\third\\bill99\\81231006300107890.jks");
		paramMap.put("cert_password", "vpos123");
		paramMap.put("merchant_id", "812310063001078");
		paramMap.put("merchant_login_key", "vpos123");
		paramMap.put("version", "1.0");
		return paramMap;
	}

}
